﻿package net.pezzato.grpbannr{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.LoaderInfo;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import com.adobe.webapis.flickr.methodgroups.Pools;
	import com.adobe.webapis.flickr.methodgroups.Photos;
	import com.adobe.webapis.flickr.FlickrService;
	import com.adobe.webapis.flickr.PhotoSize;
	import com.adobe.webapis.flickr.Group;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	import com.adobe.webapis.flickr.PagedPhotoList;
	import net.pezzato.grpbannr.PhotoSlider;
	import net.pezzato.grpbannr.Label;
	import flash.display.Stage;
	public class GrpBannr extends MovieClip {
		private var pools_:Pools;
		private var service_:FlickrService;
		private var group_id_:String;
		private var per_page_:Number;
		private var slider_:PhotoSlider;
		private var urls_:Array;
		private var sliderContainer_:Sprite;
		private var labelsContainer_:Sprite;
		private var borders_:Sprite;
		private var groupInfo_:Group;
		private var linkBanner_:Sprite;
		private static  const API_KEY:String = "67eea83f1fd7577e4d9afb18fccef21b";
		public function GrpBannr() {
			urls_ = new Array;
			group_id_ = root.loaderInfo.parameters.group_id;
			per_page_ = root.loaderInfo.parameters.per_page;
			if (group_id_ == null) {
				group_id_ =  "991990@N23";
			}
			if (isNaN(per_page_)) {
				per_page_ = 10;
			}
			trace(per_page_);
			service_ = new FlickrService(API_KEY);
			service_.addEventListener( FlickrResultEvent.GROUPS_POOLS_GET_PHOTOS, onPoolsGetPhotos );
			service_.addEventListener( FlickrResultEvent.GROUPS_GET_INFO, onGroupGetInfo );
			service_.addEventListener( FlickrResultEvent.PHOTOS_GET_SIZES, onPhotosGetSizes );
			service_.pools.getPhotos(group_id_, "", "", per_page_, 1);
			service_.groups.getInfo(group_id_);
			sliderContainer_ = new Sprite();
			addChild(sliderContainer_);
			labelsContainer_ = new Sprite();
			addChild(labelsContainer_);
			linkBanner_ = new Sprite();
			addChild(linkBanner_);
			borders_ = new Sprite();
			addChild(borders_);
			drawBorders();
		}
		private function onPoolsGetPhotos(event:FlickrResultEvent) {
			var photos:Array = event.data.photos.photos;
			for (var i:Number = 0; i < photos.length; ++i) {
				trace(photos[i].id);
				service_.photos.getSizes(photos[i].id);
			}
		}
		private function onPhotosGetSizes(event:FlickrResultEvent) {
			var photoSizes:Array = event.data.photoSizes;
			urls_.push(photoSizes[1].source);
			if (urls_.length == per_page_) {
				onAllSizesAvailables();
			}
		}
		private function onAllSizesAvailables() {
			slider_ = new PhotoSlider(stage.stageWidth, stage.stageHeight);
			slider_.addPhotosFromUrlArray(urls_);
			sliderContainer_.addChild(slider_);
		}
		private function onGroupGetInfo(event:FlickrResultEvent) {
			groupInfo_ = event.data.group;
			var nameLabel:Label = new Label(groupInfo_.name);
			labelsContainer_.addChild(nameLabel);
			nameLabel.x = 2;
			nameLabel.y = 2;
			var membersLabel:Label = new Label(String(groupInfo_.members) + " members");
			labelsContainer_.addChild(membersLabel);
			membersLabel.width *= 0.7;
			membersLabel.height *= 0.7;
			membersLabel.x = stage.stageWidth - 1 - membersLabel.width;
			membersLabel.y = stage.stageHeight - 1 - membersLabel.height;
			linkBanner_.addChild(new LinkBanner("http://www.flickr.com/groups/" + groupInfo_.nsid, stage));
		}
		private function drawBorders() {
			borders_.graphics.lineStyle(9, 0x000000, 1, true);
			borders_.graphics.drawRoundRect(-1, -1, stage.stageWidth, stage.stageHeight, 4, 4);
		}
	}
}